package com.example.blog2023.controller;

import com.example.blog2023.bean.vo.AdminBlogsVo;
import com.example.blog2023.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;

/**
 * @author cfy
 * @date 2023/12/8
 **/
@Controller
@RequestMapping("/admin")
public class BlogsController {
    @Autowired
    private AdminService adminService;

    /**
     *管理后台
     * @return
     */
    @GetMapping("/blogs")
    public ModelAndView blogs(){
        ArrayList<AdminBlogsVo> allBlogs = adminService.getAllBlogs();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("allBlogs",allBlogs);
        modelAndView.setViewName("/admin/blogs");
        return  modelAndView;
    }

    /**
     * 管理后台 搜索
     * @param title
     * @param gender
     * @return
     */
    @PostMapping("/blogs")
    public ModelAndView blogs(@RequestParam(value = "title") String title,
                              @RequestParam(value = "gender",required = false) String gender,
                              @RequestParam(value = "recommend",required = false) String recommend){
        ArrayList<AdminBlogsVo> allBlogs = adminService.getBlogsByCondition(title,gender,recommend);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("allBlogs",allBlogs);
        modelAndView.setViewName("/admin/blogs");
        return  modelAndView;
    }
    @GetMapping("/delete")
    public RedirectView deleteBolg(Integer id){
        adminService.deleteById(id);
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("/admin/blogs");
        return  redirectView;
    }
    @GetMapping("/blogs-input")
    public ModelAndView pageJump(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/blogs-input");
        return modelAndView;
    }
}
